<?
function getContactsByProgram($adminEdit=null) {
	global $dbname, $db, $db_selected;
	$contactString = '';
	
	if ($adminEdit == 1) {
		$edit = 'edit';
	} else {
		$edit = null;
	}
	
	$sql = "SELECT * from casa_programs ORDER BY ProgramName ASC";
	$result = mysql_db_query($dbname,$sql);
	
	while ($value = mysql_fetch_assoc($result)) {
		$pID      = $value[PID];
		$pName    = stripslashes($value[ProgramName]);
		$pCounty  = stripslashes($value[County]);
		$pAddress = $value[Address];
		$pCity    = $value[City];
		$pZip     = $value[Zip];
		
		if ($edit == 'edit') {
			$contactString .= '<div id="programInfo'.$pID.'"><div style="float: left;">';
			$contactString .= '<a href="#" onClick="programEditObj.editProgram('.$pID.'); return false;" title="Edit program information"><img src="images/iconEditSmall.gif" style="border: none;" /></a>&nbsp;';
			$contactString .= '</div><div style="float: left;">';
			$contactString .= '<h1 id="programName'.$pID.'">'.$pName.'</h1>';
			$contactString .= '</div><div style="clear: left;"></div></div>';
			$contactString .= '<div id="programForm'.$pID.'" style="display: none;"></div>';
		} else {
			$contactString .= '<h1>'.$pName.'</h1>';								
		}
		$sql2 = "SELECT CU.UID,CU.NameFirst,CU.NameLast,CU.Email,CU.Phone1,CU.Phone2,CU.OfficeNo,CU.PositionID,CU.Public,C.CatName AS Position 
				FROM casa_users CU 
				LEFT JOIN casa_categories C ON CU.PositionID = C.CID
				WHERE 
					CU.ProgramID = '$pID' AND
					CU.EventReg <> 1
					ORDER BY CU.NameLast";		
		$result2 = mysql_db_query($dbname,$sql2);
		$rows2 = mysql_num_rows($result);
		
		$userArray = array();
		if (mysql_num_rows($result)>0) {	
			$i=0;
			while ($value2 = mysql_fetch_assoc($result2)) {
				$userArray[$i][0] = $value2[UID];
				$userArray[$i][1] = stripslashes($value2[NameFirst]);
				$userArray[$i][2] = stripslashes($value2[NameLast]);
				$userArray[$i][3] = $value2[Email];
				$userArray[$i][4] = $value2[Phone1];
				$userArray[$i][5] = $value2[Phone2];
				$userArray[$i][6] = $value2[OfficeNo];
				$userArray[$i][7] = $value2[PositionID];
				$userArray[$i][8] = $value2[Position];
				$userArray[$i][9] = $programID;
				$userArray[$i][11]= $value2['Public'];
				$i++;
			} // End WHILE
	
			$i=$i-1;
			$rows = ceil($i/2);
			$c=0;
			
			$contactString .= '<table style="width: 100%;">';
			for($a=0;$a<=$rows;$a++) {		
				$contactString .= '<tr>';
				for($b=1;$b<=2;$b++) {
					if (!empty($userArray[$c])) {
						$contactString .= '<td style="vertical-align: top; width: 50%;">';
						$contactString .= renderUserDetails($userArray[$c],'table','',$edit);
						$contactString .= '</td>';
					} else {
						$contactString .= '<td style="vertical-align: top; width: 50%;">';
						$contactString .= '</td>';
					}	
					$c++;
				}
				$contactString .= '</tr>';		
			}
			$contactString .= '</table>';
		} else {
			$contactString .= 'There are no contacts listed for this program yet.';
		}
	}
	
	return $contactString;
}

function getContactsByLastName() {
	global $dbname, $db, $db_selected;
	$contactString = '';
		
	$sql2 = "SELECT 
				CU.UID,CU.NameFirst,CU.NameLast,CU.Email,CU.Phone1,CU.Phone2,
				CU.OfficeNo,CU.PositionID,C.CatName AS Position,
				CP.ProgramName AS ProgramName
			FROM casa_users CU 
			LEFT JOIN casa_categories C ON CU.PositionID = C.CID
			LEFT JOIN casa_programs CP ON CP.PID = CU.ProgramID
			ORDER BY CU.NameLast";
	$result2 = mysql_db_query($dbname,$sql2);
	$rows2 = mysql_num_rows($result2);
	
	if ($rows2>0) {	
		$i=0;
		while ($value2 = mysql_fetch_assoc($result2)) {
			$userArray[$i][0] = $value2[UID];
			$userArray[$i][1] = stripslashes($value2[NameFirst]);
			$userArray[$i][2] = stripslashes($value2[NameLast]);
			$userArray[$i][3] = $value2[Email];
			$userArray[$i][4] = $value2[Phone1];
			$userArray[$i][5] = $value2[Phone2];
			$userArray[$i][6] = $value2[OfficeNo];
			$userArray[$i][7] = $value2[PositionID];
			$userArray[$i][8] = $value2[Position];
			$userArray[$i][9] = $programID;
			$userArray[$i][10]= $value2[ProgramName];
						
			$i++;
		} // End WHILE
		$i=$i-1;
		
		$contactString .= '<table class="dataTable" style="width: 100%;">';
		$contactString .= '<tr>';
		$contactString .= '<th>Name</th>';
		$contactString .= '<th>Position</th>';
		$contactString .= '<th>Program</th>';
		$contactString .= '<th>Email</th>';
		$contactString .= '<th>Phone 1</th>';
		$contactString .= '<th>Office No.</th>';
		$contactString .= '</tr>';
		
		for($a=0;$a<=$i;$a++) {	
			if (($a % 2) > 0) { $class = 'altRow'; }
			else { $class = ''; }
			$contactString .= renderUserDetails($userArray[$a],'grid',$class);
		}	
		$contactString .= '</table>';
	} else {
		$contactString .= 'There are no contacts listed for this program yet.';
	}
	
	return $contactString;
}

function getContactsForProgram($pID) {
	if ($_SESSION['userLevel'] == 1) {
		$contactString = getContactsByProgram(1);
	} else {
		global $dbname, $db, $db_selected;
		$contactString = '';	
		
		$sql = "SELECT CU.UID,CU.NameFirst,CU.NameLast,CU.Email,CU.Phone1,CU.Phone2,CU.OfficeNo,CU.PositionID,CU.Public,C.CatName AS Position 
				FROM casa_users CU 
				LEFT JOIN casa_categories C ON CU.PositionID = C.CID
				WHERE CU.ProgramID = '$pID' ORDER BY CU.NameLast";	
		$result = mysql_db_query($dbname,$sql);
		$rows = mysql_num_rows($result);
	
		if (mysql_num_rows($result)>0) {	
			$i=0;
			while ($value2 = mysql_fetch_assoc($result)) {
				$userArray[$i][0] = $value2[UID];
				$userArray[$i][1] = stripslashes($value2[NameFirst]);
				$userArray[$i][2] = stripslashes($value2[NameLast]);
				$userArray[$i][3] = $value2[Email];
				$userArray[$i][4] = $value2[Phone1];
				$userArray[$i][5] = $value2[Phone2];
				$userArray[$i][6] = $value2[OfficeNo];
				$userArray[$i][7] = $value2[PositionID];
				$userArray[$i][8] = $value2[Position];
				$userArray[$i][9] = $programID;
				$userArray[$i][11]= $value2['Public'];
			
				$i++;
			} // End WHILE
		
			$i=$i-1;
			$rows = ceil($i/2);
			$c=0;
		
			$contactString .= '<table style="width: 100%;">';
			for($a=0;$a<=$rows;$a++) {		
				$contactString .= '<tr>';
				for($b=1;$b<=2;$b++) {
					if (!empty($userArray[$c])) {
						$contactString .= '<td style="vertical-align: top; width: 50%;">';
						$contactString .= renderUserDetails($userArray[$c],'table','','edit');
						$contactString .= '</td>';
					}	
					$c++;
				}
				$contactString .= '</tr>';		
			}
			$contactString .= '</table>';
		} else {
			$contactString .= 'There are no contacts listed for this program yet.';
		}
	}	
	
	return $contactString;
}

function renderUserDetails($userArray,$layout=null,$rowClass=null,$mode=null) {
	$returnString = '';
	
	$uID         = $userArray[0];
	$nameFirst   = $userArray[1];
	$nameLast    = $userArray[2];
	$email       = $userArray[3];
	$phone1      = $userArray[4];
	$phone2      = $userArray[5];
	$officeNo    = $userArray[6];
	$positionID  = $userArray[7];
	$position    = $userArray[8];
	$programID   = $userArray[9];
	$programName = $userArray[10];
	$public      = $userArray[11];
	
	if ($officeNo == 0) {
		$officeNo = '';
	}
	if ($phone1 == 0) {
		$phone1 = '';
	}
	
	if ( empty($layout) || ($layout == 'table') ) {
		$returnString .= '<div id="contactContainer'.$uID.'">';
		$returnString .= '<div style="padding: 2px; background: #EBF4FD;">';
			if ($mode == 'edit') {
				$returnString .= '<a href="#" onClick="confirmDelete('.$uID.',\'contact\'); return false;" title="Delete this contact"><img src="images/iconDeleteSmall.gif" style="border: none;" /></a>&nbsp;';
				$returnString .= '<a href="#" onClick="contactsEditObj.editContact('.$uID.'); return false;" title="Edit this contact"><img src="images/iconEditSmall.gif" style="border: none;" /></a>&nbsp;';
			}
			$returnString .= '<b>'.$nameFirst.' '.$nameLast.'</b>';
		$returnString .= '</div>';
		$returnString .= '<div style="padding: 2px;">';
		if (!empty($position)) {
			$returnString .= $position.'<br />';
		}
		if (!empty($email)) {
			$returnString .= '<a href="mailto:'.$email.'">'.$email.'</a><br />';
		}
		if (!empty($phone1)) {
			$returnString .= 'Office: '.$phone1.'<br />';
		}
		if (!empty($phone2)) {
			$returnString .= 'Mobile, Other: '.$phone2.'<br />';
		}
		if (!empty($officeNo)) {
			$returnString .= 'Office Number: '.$officeNo.'<br />';
		}
		if ($public == 1) {
			$returnString .= '<div style="margin-top: 3px;"><img src="images/iconCheckGreenOn.gif" /> Public Contact</div>';
		}
		$returnString .= '</div></div><br />';
		$returnString .= '<div id="contactContainerTemp'.$uID.'" style="display: none;"></div>';
	} elseif ($layout == 'grid') {
		$returnString .= '<tr id="contactContainer'.$uID.'" class="'.$rowClass.'">';
		$returnString .= '<td>'.$nameLast.', '.$nameFirst.'</td>';
		$returnString .= '<td>'.$position.'</td>';
		$returnString .= '<td>'.$programName.'</td>';
		$returnString .= '<td><a href="mailto:'.$email.'">'.$email.'</a></td>';
		$returnString .= '<td>'.$phone1.'</td>';
		$returnString .= '<td>'.$officeNo.'</td>';
		$returnString .= '</tr>';		
	}
	
	return $returnString;
}

function saveContact($phpString) {
	global $dbname, $db, $db_selected;
	
	// Parse phpString
	$phpStringArray = explode('|',$phpString);
		    
	$uID = $phpStringArray[0];
	$action = $phpStringArray[1];
	$nameFirst = fieldToDB($phpStringArray[2]);  
	$nameLast  = fieldToDB($phpStringArray[3]);    
	$phone1    = fieldToDB($phpStringArray[4]);
	$phone2    = fieldToDB($phpStringArray[5]);
	$email     = fieldToDB($phpStringArray[6]);
	$officeNo  = fieldToDB($phpStringArray[7]);
	$position  = $phpStringArray[8];
	$public    = $phpStringArray[9];
	$programID = $_SESSION[programID];
	
	if ($phone1 == 'Phone 1') {
		$phone1 = '';
	}
	if ($phone2 == 'Phone 2') {
		$phone2 = '';
	}
	
	if ($action == 'add') {
		$sql = "INSERT into casa_users (
					ProgramID,
					NameFirst,
					NameLast,
					Email,
					Phone1,
					Phone2,
					OfficeNo,
					PositionID,
					AdminLevel,
					DateAdded,
					Public		
				) values (
					'$programID',
					'$nameFirst',
					'$nameLast',
					'$email',
					'$phone1',
					'$phone2',
					'$officeNo',
					'$position',
					'0',
					'".date('Y-m-d')."',
					'$public'
				)";
	} elseif ($action == 'edit') {
		$sql = "UPDATE casa_users SET
					NameFirst = '$nameFirst',
					NameLast  = '$nameLast',
					Email     = '$email',
					Phone1    = '$phone1',
					Phone2    = '$phone2',
					OfficeNo  = '$officeNo',
					PositionID= '$position',
					DateUpdate= '".date('Y-m-d')."',
					Public    = '$public'
				WHERE
				UID = '$uID'";
	}
	$result = mysql_db_query($dbname,$sql);
	
	$returnString = getContactsForProgram($programID);
	return $returnString;
}

function deleteContact($uID) {
	global $dbname, $db, $db_selected;

	$sql = "DELETE from casa_users WHERE UID = '$uID'";
	$result = mysql_db_query($dbname,$sql);
	
	return $uID;
}

function getContactForEdit($uID) {
	global $dbname, $db, $db_selected;
	$returnString = '';
	
	$sql = "SELECT * from casa_users WHERE UID = '$uID'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	
	$programID = $row[ProgramID];
	$nameFirst = $row[NameFirst];
	$nameLast  = $row[NameLast];
	$email     = $row[Email];
	$phone1    = $row[Phone1];
	$phone2    = $row[Phone2];
	$officeNo  = $row[OfficeNo];
	$positionID= $row[PositionID];
	$public    = $row['Public'];
	
	$returnString .= $uID.'|'.$programID.'|'.$nameFirst.'|'.$nameLast.'|'.$email.'|';
	$returnString .= $phone1.'|'.$phone2.'|'.$officeNo.'|'.$positionID.'|'.$public;
	return $returnString;	
}

function saveProgram($phpString) {
	global $dbname, $db, $db_selected;
	
	// Parse PHP array
	$phpArray = explode('|',$phpString);
	
	$pID      = $phpArray[0];
	$pName    = $phpArray[1];
	$pCounty  = $phpArray[2];
	$pAddress = $phpArray[3];
	$pCity    = $phpArray[4];
	$pZip     = $phpArray[5];
	$pURL     = $phpArray[6];
	$pPhone1  = $phpArray[7];
	$pPhone2  = $phpArray[8];
	$pEmail   = $phpArray[9];
	
	// Save data to appropriate tables
	$sql1 = "UPDATE casa_users SET
				Email  = '$pEmail',
				Phone1 = '$pPhone1',
				Phone2 = '$pPhone2' 
			WHERE 
				ProgramID  = '$pID' AND
				AdminLevel = 2";
	$result1 = mysql_db_query($dbname,$sql1);
	
	$sql2 = "UPDATE casa_programs SET
				ProgramName = '$pName',
				County      = '$pCounty',
				Address     = '$pAddress',
				City        = '$pCity',
				Zip         = '$pZip',
				URL         = '$pURL'	 
			WHERE 
				PID  = '$pID'";
	$result2 = mysql_db_query($dbname,$sql2);
	
	return $pID.'|'.$pName;
}
?>